<?php
/**
 * Created by PhpStorm.
 * User: man0sions
 * Date: 16/8/25
 * Time: 下午5:58
 * 控制器
 */

namespace src\controllers;

use src\base\ApplicationHelper;
use src\base\CommandResolver;
use src\base\Request;
use src\base\SpiderContext;

/**
 * 前端控制器模式实现命令处理
 * Class Controller
 * @package src\controllers
 * @author Luficer.p <81434146@qq.com>
 */
class Controller{
    /**
     * @var
     */
    private $applicationHelper;
    /**
     * @var
     */
    private static $instance;

    private function __construct()
    {
    }
    static function run(){
        if(!self::$instance)
            self::$instance = new self();

        self::$instance->init();
        self::$instance->handleRequest();
    }

    /**
     * 初始化操作
     *
     */
    private function init(){
        $this->applicationHelper = ApplicationHelper::getInstance();
        $this->applicationHelper->init();
    }


    /**
     * 执行命令
     */
    private function handleRequest(){
        $request = Request::getInstance();
        $cmd_r = new CommandResolver($request);
        $cmd = $cmd_r->getCommand();
        $context = new SpiderContext($request);
        $cmd->execute($context);


    }



}